package com.region.integral.business_user.consume.dao.impl;

import java.util.List;

import org.springframework.stereotype.Component;

import com.region.integral.business_user.consume.dao.ICheckNo;
import com.region.integral.business_user.pojo.BUIEntity;
import com.region.integral.business_user.xml.Regulation;
import com.region.integral.business_user.xml.dto.CheckNo;
import com.region.integral.common_user.obtain.vo.IntegralData;
import com.region.integral.exception.IntegralException;
import com.region.integral.hibernate.BaseHibernate;
import com.region.integral.util.BusinessUserLogUtil;
import com.region.integral.util.XMLRegulationUtil;
@Component("iCheckNo")
public class CheckNoImpl extends BaseHibernate implements ICheckNo {

	@Override
	public void checkNo(int businessUserid, int noNum,
			IntegralData integralData) throws IntegralException {
		BUIEntity buiEntity = getBUIEntity(businessUserid);
		if (buiEntity != null) {
			int currIntegral = buiEntity.getCurrIntegral();
			Regulation regulation = XMLRegulationUtil.unmarshaller2();
			List<CheckNo> checkNos = regulation.getCheckNo();
			for (CheckNo checkNo : checkNos) {
				int ruleIntegral = checkNo.getIntegral();
				if(noNum!=0&&noNum%checkNo.getRate()==0&&currIntegral >= Math.abs(ruleIntegral)){
					int totalIntegral = currIntegral + ruleIntegral;
					buiEntity.setCurrIntegral(totalIntegral);
					hibernateTemplate.update(buiEntity);
					hibernateTemplate.save(BusinessUserLogUtil.log(businessUserid, ruleIntegral, "checkNo"));
					integralData.setSuccee(true);
				}
				else{
					integralData.setSuccee(false);
				}
			}

		}
		
	}

}
